<?php

namespace app\modules\admin\controllers;

use Yii;
use app\models\Settings;
use app\modules\admin\controllers\AdminController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * SettingController implements the CRUD actions for settings model.
 */
class SettingController extends AdminController {

    public $enableCsrfValidation = false;

    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all settings models.
     * @return mixed
     */
    public function actionIndex() {
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();

            $this->__save_setting('site_name', $post['site_name']);
            $this->__save_setting('site_slogan', $post['site_slogan']);
            $this->__save_setting('site_url', $post['site_url']);
            $this->__save_setting('site_logo', $post['site_logo']);
            $this->__save_setting('site_cpoyright', $post['site_cpoyright']);
            $this->__save_setting('tongji', $post['tongji']);
            $session = Yii::$app->session;
            $session->setFlash('message', '保存成功');
            $this->redirect('/admin/setting');
        } else {
            $site_name = Settings::findOne(['name' => 'site_name']);
            $site_slogan = Settings::findOne(['name' => 'site_slogan']);
            $site_url = Settings::findOne(['name' => 'site_url']);
            $site_logo = Settings::findOne(['name' => 'site_logo']);
            $site_cpoyright = Settings::findOne(['name' => 'site_cpoyright']);
            $tongji = Settings::findOne(['name' => 'tongji']);

            $settings = array(
                'site_name' => $site_name,
                'site_slogan' => $site_slogan,
                'site_url' => $site_url,
                'site_logo' => $site_logo,
                'site_cpoyright' => $site_cpoyright,
                'tongji' => $tongji,
            );
            return $this->render('index', $settings);
        }
    }

    /**
     * Creates a new settings model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new settings();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->name]);
        } else {
            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing settings model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->name]);
        } else {
            return $this->render('update', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing settings model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the settings model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return settings the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = settings::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    protected function __save_setting($name, $value) {
        $setting = Settings::findOne(['name' => $name]);
        $setting->value = $value;
        $setting->save();
    }

}
